package com.wxj.bank.api.service.impl;

import com.wxj.bank.api.service.IUserBusinessService;
import com.wxj.bank.module.constant.Constants;
import com.wxj.bank.module.enums.ExceptionEnum;
import com.wxj.bank.module.exceptions.BankException;
import com.wxj.bank.module.modle.dto.UserOrder;
import com.wxj.bank.module.page.PageBean;
import com.wxj.bank.module.utils.JsonUtils;
import com.wxj.bank.module.utils.RedisKeyUtils;
import com.wxj.bank.storage.model.Category;
import com.wxj.bank.storage.model.Order;
import com.wxj.bank.storage.model.OrderUser;
import com.wxj.bank.storage.model.Product;
import com.wxj.bank.storage.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author shengtao
 * @Description:
 * @date 2019/02/20 16:25
 */
@Service
public class UserBusinessServiceImpl implements IUserBusinessService{

    @Autowired
    private IUserService userService;
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IProductService productService;
    @Autowired
    private ICategoryService categoryService;
    @Autowired
    private IRedisService redisService;

    @Override
    @Transactional
    public OrderUser add(OrderUser orderUser) {
        orderUser = userService.save(orderUser);
        String key = RedisKeyUtils.genKey(Constants.REDIS_KEY_USER,orderUser.getId());
        redisService.set(key, JsonUtils.obj2Json(orderUser));
        return orderUser;
    }

    @Override
    public PageBean<OrderUser> find(OrderUser user, Integer currentPage, Integer pageSize) {
        PageBean<OrderUser> pageBean = new PageBean<>(currentPage,pageSize,0L);
        return userService.find(user,pageBean);
    }

    @Override
    public List<UserOrder> findOrders(String userId) {
        List<UserOrder> orderUsers = null;
        if(StringUtils.isBlank(userId)){
            throw new BankException(ExceptionEnum.PARAM_VALID);
        }
        OrderUser orderUser = findById(userId);
        if(orderUser == null){
            throw new BankException(ExceptionEnum.PARAM_VALID);
        }
        Order order = new Order();
        order.setUserId(userId);
        List<Order> orders = orderService.find(order);
        if(CollectionUtils.isEmpty(orders)){
            throw new BankException(ExceptionEnum.NOT_EXISTS_USER_ORDER);
        }
        orderUsers = new ArrayList<>();
        for (Order order1:orders){
            UserOrder userOrder = new UserOrder();
            userOrder.setNikeName(orderUser.getNikeName());
            userOrder.setDate(order1.getDate());
            userOrder.setOrderNum(order1.getNum());
            Product product = productService.find(orders.get(0).getProductId());
            if(product != null){
                userOrder.setpName(product.getName());
            }
            userOrder.setPrice(product.getPrice());
            Category category = categoryService.find(product.getCategoryId());
            if(category != null){
                userOrder.setPcName(category.getName());
            }
            orderUsers.add(userOrder);
        }
        return orderUsers;
    }

    public OrderUser findById(String userId){
        String key = RedisKeyUtils.genKey(Constants.REDIS_KEY_USER,userId);
        OrderUser orderUser = redisService.get(key,OrderUser.class);
        if(orderUser == null){
            return userService.find(userId);
        }
        return orderUser;
    }
}
